package com.homni.iomix_xair.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.homni.iomix_xair.R;

public class VerticalSeekbar extends View {
	
	private static final String TAG="VerticalSeekBarXJP";	
	private  int offset;
		
	private Drawable thumbDrawable;

	private float defaultProgress;
	private int thumWidth;
	private int thumHeight;
	//[0,99]
	private float mProgress;
	
	private Bitmap thum;
	
	private float  
	//bgWidth, bgHeight,
	widthspe, heightspe;
	
	private Paint paint = new Paint();
	private float dy, 
	tempYPos;
	
	private int mMax=100;
	private float[] everyPartYPoss =new float[mMax];
	
	private OnVerticalSeekBarListener onVerticalListener;
	private Rect /*rectBgSrc=new Rect(),rectBgDst=new Rect(),
			rectThSrc=new Rect(),*/rectThDst=new Rect();
	private PaintFlagsDrawFilter pfd;
	
	public interface OnVerticalSeekBarListener
	{
   	   	public void onProgressChanged(VerticalSeekbar vBar, float progress);
  
   		public void onStartTrackingTouch(VerticalSeekbar vBar);

		public void onStopTrackingTouch(VerticalSeekbar vBar, float progress);
	}
	
	public VerticalSeekbar(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.verticalSeek);
		defaultProgress = typedArray.getInt(R.styleable.verticalSeek_defaultValue, 0);
		
		thumbDrawable = typedArray.getDrawable(R.styleable.verticalSeek_thumb);
		typedArray.recycle();
		BitmapDrawable thBitmap = (BitmapDrawable) thumbDrawable;
		thum = thBitmap.getBitmap();
		
		setThumSize(); 
		
		setOffset(-thumHeight / 2);
		
		mProgress = defaultProgress;
		pfd = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
	}
	
	public void setOnVerticalListener(OnVerticalSeekBarListener listener)
	{
		onVerticalListener = listener;
	}
	
	private void onStartTrackingTouch()
	{
		if (onVerticalListener != null)
		{
			onVerticalListener.onStartTrackingTouch(this);
		}
	}
    
	private void onStopTrackingTouch()
	{
		if (onVerticalListener != null)
		{
			onVerticalListener.onStopTrackingTouch(this,mProgress);
		}
	}
    
	private void onProgressChanged(VerticalSeekbar vBar, float progress)
	{
		if (onVerticalListener != null)
		{
			onVerticalListener.onProgressChanged(this, mProgress);
		}
	}
   
	@Override
	protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
	{
	
		/******************************************/
		widthspe = MeasureSpec.getSize(widthMeasureSpec);
		heightspe = MeasureSpec.getSize(heightMeasureSpec);
		setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
		
		int blank = (int) ((float) heightspe * (float) 25 / (float) 349);
		
		/*************************************************************************************************/			
		float viewHeight=heightspe - blank - thumHeight;		
		int length = everyPartYPoss.length;
		float avg100=viewHeight / 100;		
		for (int i = 0; i < length; i++)
		{
			everyPartYPoss[i] = (int) ((i+1) *avg100);
		}				
	}

	public void setThumSize()
	{
		thumWidth = (int) (thum.getWidth());
		thumHeight = (int) (thum.getHeight());
	}

	public void setOffset(int offset)
	{
		this.offset = offset;
	}

	@Override
	protected synchronized void onDraw(Canvas canvas) 
	{
		tempYPos=everyPartYPoss[(int)mProgress];
      
		/*rectBgSrc.left=0;
		 rectBgSrc.top=0;
		 rectBgSrc.right=(int) bgWidth;
		 rectBgSrc.bottom= (int) bgHeight;
		 rectBgDst.left=0;
		 rectBgDst.top=0;
		 rectBgDst.right= (int) widthspe;
		 rectBgDst.bottom=(int) heightspe;
		 */		
		
		/*rectThSrc.top=0;
		rectThSrc.left=0;
		rectThSrc.right=thumWidth;
		rectThSrc.bottom=thumHeight;*/
		rectThDst.top=(int) tempYPos + 20;
		rectThDst.left=(int) ((widthspe - thumWidth) / 2);
		
		rectThDst.right=(int) (thumWidth + rectThDst.left);
		rectThDst.bottom=(int)(thumHeight + rectThDst.top);
		
		/*
   (left,top)		
		          (right,bottom)
		--------------
		|            |
		|            |
		|            |
		|            |
		--------------
		*/
		
		canvas.setDrawFilter(pfd);
		
		canvas.drawBitmap(thum, null/*rectThSrc*/,rectThDst, paint);
		canvas.setDrawFilter(null);
	}		
	
	@SuppressLint("ClickableViewAccessibility")
	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		// TODO Auto-generated method stub
		switch (event.getAction())
		{
		case MotionEvent.ACTION_DOWN:
			// TODO
			// Log.w(TAG, "  seeb bar xjp ACTION_DOWN");
			onStartTrackingTouch();
			dy = event.getY() + offset;

			if (dy < (heightspe - thumHeight - 20) & dy > 20)
			{
				tempYPos = event.getY() + offset;
			}
			// break;

			// Log.w(TAG, "  seeb bar xjp ACTION_MOVE");
			if (event.getY() + offset < 0)
			{
				Log.e(TAG, "event.getY() + offset < 0");
				mProgress = 0;
				tempYPos = everyPartYPoss[(int)mProgress];
			} else if (event.getY() + offset > (heightspe - thumHeight))
			{
				mProgress = 99;
				tempYPos = everyPartYPoss[(int)mProgress];
			} else
			{
				mProgress = (int) (((event.getY() + offset) / ((heightspe - thumHeight) / 100)));

				if (mProgress > 99)// max-1
				{
					mProgress = 99;// max-1
				} else if (mProgress < 0)
				{
					mProgress = 0;
				}

				tempYPos = everyPartYPoss[(int)mProgress];
			}
			onProgressChanged(this, mProgress);
			invalidate();
			break;
		case MotionEvent.ACTION_MOVE:
			// Log.w(TAG, "  seeb bar xjp ACTION_DOWN");
			// onStartTrackingTouch();
			dy = event.getY() + offset;

			if (dy < (heightspe - thumHeight - 20) & dy > 20)
			{
				tempYPos = event.getY() + offset;
			}
			// break;

			// Log.w(TAG, "  seeb bar xjp ACTION_MOVE");
			if (event.getY() + offset < 0)
			{
				Log.e(TAG, "event.getY() + offset < 0");
				mProgress = 0;
				tempYPos = everyPartYPoss[(int)mProgress];
			} else if (event.getY() + offset > (heightspe - thumHeight))
			{
				mProgress = 99;
				tempYPos = everyPartYPoss[(int)mProgress];
			} else
			{
				mProgress = ((event.getY() + offset) / ((heightspe - thumHeight) / 100));

				if (mProgress > 99)// max-1
				{
					mProgress = 99;// max-1
				} else if (mProgress < 0)
				{
					mProgress = 0;
				}

				tempYPos = everyPartYPoss[(int)mProgress];
			}
			onProgressChanged(this, mProgress);
			invalidate();
			break;
		case MotionEvent.ACTION_CANCEL:
			invalidate();
			break;
		case MotionEvent.ACTION_UP:
			onStopTrackingTouch();
			invalidate();
			break;
		}
		return true;
	}
	
	public void setProgress(float progress)
	{
		mProgress = progress;
		invalidate();
	}

	public float getProgress()
	{
		return mProgress;
	}
}
